function [M,S1,S2,N,C,NPM,CPM] = nucAndCellPM2CellMask(ncPM,clPM)

sigma = 3;

P = ncPM;
% for i = 1:3
%     P(:,:,i) = imopen(P(:,:,i),strel('disk',sigma,0));
% end
FGProbMap = normalize(imgaussfilt(P(:,:,3),sigma)); NPM = FGProbMap;
BGMask = normalize(imgaussfilt(P(:,:,1),sigma)) > 0.4;
BDProbMap = normalize(imgaussfilt(P(:,:,2),sigma));

% F = filterLoG(imresize(FGProbMap,0.25),5);
% F = imresize(normalize(F.*(F > 0)),size(FGProbMap));
% FGProbMap = FGProbMap.*F;

S = normalize((1-FGProbMap).*BDProbMap);
S = imhmin(S,0.01); S1 = S;
S(BGMask) = -Inf;
S(:,1) = -Inf; S(:,end) = -Inf; S(1,:) = -Inf; S(end,:) = -Inf;
W = watershed(S) > 1;

W = bwareafilt(W,[50 Inf]); N = W;
% W = bwmorph(W,'shrink',Inf);
% D = normalize(bwdist(W));

% imshowLinkedTuple(NPM,S1,N)
% return
    
% sigma = 5;

P = clPM;
% for i = 1:3
%     P(:,:,i) = imopen(P(:,:,i),strel('disk',sigma,0));
% end
FGProbMap = normalize(imgaussfilt(P(:,:,3),sigma)); CPM = FGProbMap;
BGMask = normalize(imgaussfilt(P(:,:,1),sigma)) > 0.4;
BDProbMap = normalize(imgaussfilt(P(:,:,2),sigma));

% F = filterLoG(imresize(FGProbMap,0.25),5);
% F = imresize(normalize(F.*(F > 0)),size(FGProbMap));
% FGProbMap = FGProbMap.*F;

S = normalize((1-FGProbMap).*BDProbMap);
% S = normalize((1-FGProbMap).*BDProbMap);
S = imhmin(S,0.01); S2 = S;
S(BGMask) = -Inf;
S(:,1) = -Inf; S(:,end) = -Inf; S(1,:) = -Inf; S(end,:) = -Inf;

W = watershed(S) > 1; C = W;
W = filterByContact(W,bwmorph(BGMask,'erode',2));
M = selectByContact(W,N);

% imshowLinkedTuple(NPM,S1,N,CPM,S2,C,M)
% imshowLinkedTuple(S2,C,W,M)
    
end